package com.tebiecloud.store.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.store.client.model.entity.StoreProfession;
import com.tebiecloud.store.server.service.StoreProfessionService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 门店的业务 前端控制器
 * @author tebie
 * @date 2019-09-15
 */
@RestController
@RequestMapping("/storeprofession")
public class StoreProfessionController {

    @Autowired
    private StoreProfessionService storeProfessionService;

    /**
     * 获取所有店铺分类
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody<List<StoreProfession>> list() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",0);
        queryWrapper.orderByDesc("ct");
        return ResultBodyUtils.ok(storeProfessionService.list(queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<StoreProfession> get(@RequestParam("id") Long id) {
        StoreProfession entity = storeProfessionService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param name 业务名称,
     * @param deleted 删除状态：0没有删除1已被删除
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        StoreProfession entity = new StoreProfession();
        entity.setName(name);
        entity.setDeleted(deleted);
        storeProfessionService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param name 业务名称
     * @param deleted 删除状态：0没有删除1已被删除
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "professionId", required = false) Long professionId,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "deleted", required = false) Integer deleted
    ) {
        StoreProfession entity = new StoreProfession();
        entity.setProfessionId(professionId);
        entity.setName(name);
        entity.setDeleted(deleted);
        storeProfessionService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            storeProfessionService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            storeProfessionService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
